//
//  LoginRegistNet.m
//  SportSocial
//
//  Created by yull on 15/1/5.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "LoginRegistNet.h"

@implementation LoginRegistNet

+ (AFHTTPRequestOperation *)loginWithName:(NSString *)name
                                 password:(NSString *)password
                             successBlock:(httpSuccessBlock)succesBlock
                                failBlock:(httpFailBlock)failBlock
{
    NSDictionary *dict = @{@"mobile":name,
                           @"password":password};
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/login",[self baseUrl]];
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        succesBlock(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock(operation,error);
    }];
    
    return operation;
}

+ (AFHTTPRequestOperation *)loginByQQWithAccessToken:(NSString *)token
                                              openId:(NSString *)openId
                                        successBlock:(httpSuccessBlock)succesBlock
                                           failBlock:(httpFailBlock)failBlock
{
    NSDictionary *dict = @{@"access_token":token,
                           @"openid":openId};
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/loginByQQ",[self baseUrl]];
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        succesBlock(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock(operation,error);
    }];
    
    return operation;
}

+ (AFHTTPRequestOperation *)loginByWeiboWithAccessToken:(NSString *)token
                                                    uid:(NSString *)uid
                                           successBlock:(httpSuccessBlock)succesBlock
                                              failBlock:(httpFailBlock)failBlock
{
    NSDictionary *dict = @{@"access_token":token,
                           @"uid":uid};
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/loginByWeibo",[self baseUrl]];
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        succesBlock(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock(operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)getWeixinAccessTokenWithAppid:(NSString *)appid
                                                   secret:(NSString *)secret
                                                     code:(NSString *)code
                                               grant_type:(NSString *)grant_type
                                             successBlock:(httpSuccessBlock)succesBlock
                                                failBlock:(httpFailBlock)failBlock
{
    NSString *url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=%@", appid, secret, code, grant_type];
    
//    AFHTTPRequestOperationManager *manager = [self manager];
//    [manager.requestSerializer setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
    
    AFHTTPRequestOperation *operation = [[self manager] GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        succesBlock(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock(operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)loginByWeixinWithAccessToken:(NSString *)token
                                                  openId:(NSString *)openId
                                            successBlock:(httpSuccessBlock)succesBlock
                                               failBlock:(httpFailBlock)failBlock
{
    NSDictionary *dict = @{@"access_token":token,
                           @"openid":openId};
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/loginByWeixin",[self baseUrl]];
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        succesBlock(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock(operation,error);
    }];
    
    return operation;
}

+ (AFHTTPRequestOperation *)getVerifyCodeWithPhone:(NSString *)phone
                                      successBlock:(httpSuccessBlock)succesBlock
                                         failBlock:(httpFailBlock)failBlock
{
    NSDictionary *dict = @{@"mobile":phone};
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/register/verifyCode",[self baseUrl]];
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        succesBlock(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock(operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)getForgotPWVerifyCodeWithPhone:(NSString *)phone
                                              successBlock:(httpSuccessBlock)succesBlock
                                                 failBlock:(httpFailBlock)failBlock
{
    NSDictionary *dict = @{@"mobile":phone};
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/password/verifyCode",[self baseUrl]];
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        succesBlock(operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock(operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)uploadUserHeadImage:(NSData *)picData
                                   successBlock:(httpSuccessBlock)succesBlock
                                      failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/uploadIcon",[self baseUrl]];
    NSDictionary *parm = @{@"token":[self token]};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:parm constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:picData name:@"pic" fileName:@"pic.jpg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        succesBlock (operation,responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failBlock (operation,error);
    }];
    return operation;
}

+ (AFHTTPRequestOperation *)registWithAccount:(NSString *)account
                                     password:(NSString *)password
                                        phone:(NSString *)phone
                                   verifyCode:(NSString *)verifyCode
                                 successBlock:(httpSuccessBlock)successBlock
                                    failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/register", [self baseUrl]];
    NSDictionary *params = @{@"mobile":phone, @"name":account, @"password":password, @"sex":@"0", @"verify_code":verifyCode};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:params success:successBlock failure:failBlock];
    
    return operation;
}

+ (AFHTTPRequestOperation *)resetPasswordWithPhone:(NSString *)phone
                                        verifyCode:(NSString *)verifyCode
                                       newPassword:(NSString *)newPassword
                                      successBlock:(httpSuccessBlock)successBlock
                                         failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/password/find", [self baseUrl]];
    NSDictionary *params = @{@"mobile":phone,
                             @"new_password":newPassword,
                             @"verify_code":verifyCode};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:params success:successBlock failure:failBlock];
    
    return operation;
}

+ (AFHTTPRequestOperation *)logoutSuccessBlock:(httpSuccessBlock)successBlock
                                     failBlock:(httpFailBlock)failBlock
{
    NSString *url = [[NSString alloc] initWithFormat:@"%@/user/logout", [self baseUrl]];
    NSDictionary *params = @{@"token":[CurrentUser shareInstance].token};
    
    AFHTTPRequestOperation *operation = [[self manager] POST:url parameters:params success:successBlock failure:failBlock];
    
    return operation;
}

@end
